<?php

namespace app\common\model\mysql;

use think\Model;

class SysUserRole extends Model
{
    /**
     * 根据$ids获取分配角色数据
     * @param $ids
     * @return SysPost|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function passUserRoleDelIds($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['role_id','in',$ids]
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    /**
     * 根据$role_id获取用户id数组
     * @param $role_id
     * @return void
     */
    public function getUserIds($role_id){
        if(empty($role_id)){
            return false;
        }
        $where = [
            ['role_id','=',$role_id]
        ];
        $result = $this->where($where)->column('user_id');
        return $result;
    }

    /**
     * 根据用户id获取用户role_id数组
     * @param $ids
     * @return SysPost|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getRoleIds($user_id){
        if(empty($user_id)){
            return false;
        }
        $where = [
            ['user_id','=',$user_id]
        ];
        $result = $this->where($where)->column('role_id');
        return $result;
    }

    //批量插入角色与用户关联表数据
    public function insertAllRoleUser($userIds,$roleId){
        $data = [];
        foreach ($userIds as $userId) {
            $data[] = [
                'role_id' => $roleId,
                'user_id' => $userId,
            ];
        }
        return $this->insertAll($data);
    }

    //批量插入用户与角色关联表数据
    public function insertAllUserRole($userId,$roleIds){
        $data = [];
        foreach ($roleIds as $roleId) {
            $data[] = [
                'role_id' => $roleId,
                'user_id' => $userId,
            ];
        }
        return $this->insertAll($data);
    }

    //删除角色与用户关联表单个数据
    public function deleteRoleUser($userId,$roleId){
        $where = [
            ['role_id','=',$roleId],
            ['user_id','=',$userId]
        ];
        return $this->where($where)->delete();
    }

    //批量取消角色分配用户授权
    public function deleteAllRoleUser($userIds,$roleId){
        if(empty($roleId) || empty($userIds)){
            return false;
        }
        $where = [
            ['role_id','=',$roleId],
            ['user_id','in',$userIds]
        ];
        return $this->where($where)->delete();
    }

    //批量删除用户与角色关联表
    public function deleteAllUserRole($userId){
        if(empty($userId)){
            return false;
        }
        $where = [
            ['user_id','=',$userId]
        ];
        return $this->where($where)->delete();
    }

    //批量删除用户与角色关联表
    public function deleteAllInUserRole($userId){
        if(empty($userId)){
            return false;
        }
        $userId = explode(',', $userId);
        $where = [
            ['user_id','in',$userId]
        ];
        return $this->where($where)->delete();
    }
}
